#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <linux/input.h>
#include "ui.h"
#include "touch.h"
#include "png.h"
#include "album.h"
#include "music.h"
#include "video.h"

int *lcd_ptr;
int lcd_fd, ts_fd;

// 按钮定义
Button buttons[] = {
    {60, 80, 220, 120, "相册", "/zdata/icon/album.png"},
    {290, 80, 220, 120, "音乐播放器", "/zdata/icon/music.png"},
    {520, 80, 220, 120, "影音播放器", "/zdata/icon/video.png"},
    {60, 240, 220, 120, "数据统计", "/zdata/icon/icon4.png"},
    {290, 240, 220, 120, "水质监测", "/zdata/icon/icon5.png"},
    {520, 240, 220, 120, "系统设置", "/zdata/icon/icon6.png"},
};
#define BUTTON_NUM (sizeof(buttons)/sizeof(Button))

// 绘制按钮
void draw_button(Button *btn) {
    if (btn->icon_path)
        lcd_draw_scale_png(btn->icon_path, btn->x+20, btn->y+20, 80, 80);
}

// 绘制主界面
void draw_main_ui() {
    lcd_draw_png("/zdata/icon/main_bg.png", 0, 0);
    for (int i = 0; i < BUTTON_NUM; i++) {
        draw_button(&buttons[i]);
    }
}

// 获取按钮索引
int get_button_index(int x, int y) {
    for (int i = 0; i < BUTTON_NUM; i++) {
        Button *btn = &buttons[i];
        if (x >= btn->x && x < btn->x + btn->w &&
            y >= btn->y && y < btn->y + btn->h) {
            return i;
        }
    }
    return -1;
}

// 通用模块处理函数
static int handle_module(int module_type) {
    TouchPoint p;
    int result;
    
    switch (module_type) {
        case 0: // 相册
            show_album_ui();
            while (1) {
                p = get_touch_data(ts_fd);
                if (p.valid == 2) {
                    if (handle_album_touch(p.x, p.y)) continue;
                    if (p.x > 750 && p.y > 430) break;
                }
            }
            break;
            
        case 1: // 音乐播放器
            show_music_ui();
            while (1) {
                p = get_touch_data(ts_fd);
                if (p.valid == 2) {
                    result = handle_music_touch(p.x, p.y);
                    if (result == 2) break;
                    if (result == 1) continue;
                    if (p.x > 750 && p.y > 430) break;
                }
            }
            break;
            
        case 2: // 影音播放器
            show_video_ui();
            while (1) {
                p = get_touch_data(ts_fd);
                if (p.valid == 2) {
                    if (handle_video_touch(p.x, p.y)) continue;
                    if (p.x > 750 && p.y > 430) break;
                }
            }
            break;
    }
    
    draw_main_ui();
    return 0;
}

// 处理按钮点击
void handle_button(int idx, int x, int y) {
    if (idx >= 0 && idx < BUTTON_NUM) {
        printf("点击按钮: %s\n", buttons[idx].label);
        handle_module(idx);
    }
}

// 设备初始化
int dev_init() {
    lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd == -1) {
        printf("open lcd device failed!\n");
        return -1;
    }

    lcd_ptr = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);
    if (lcd_ptr == MAP_FAILED) {
        printf("mmap failed!\n");
        return -1;
    }

    ts_fd = open("/dev/input/event0", O_RDWR);
    if (ts_fd == -1) {
        printf("open ts device failed!\n");
        return -1;
    }

    return 0;
}

// 设备反初始化
int dev_uninit() {
    munmap(lcd_ptr, 800 * 480 * 4);
    close(lcd_fd);
    close(ts_fd);
    return 0;
} 